package com.shiloop.flashlight;

import android.hardware.Camera;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

/**
 * Created by skiloop on 2014/9/4.
 */
public class Flashlight implements View.OnClickListener {
    private static final String TAG = "Flashlight";
    private boolean mIsOn;
    private Camera mCamera;
    private boolean mIsSupportFlashMode;
    private AbstractFlashView flashView;

    private ArrayList<FlashlightListener> flashlightListeners;
    private Object mListenerLock = new Object();

    public Flashlight() {
        mIsOn = false;
        mIsSupportFlashMode = false;
        flashlightListeners = new ArrayList<FlashlightListener>();
        initCamera();
    }

    public void addFlashlightListener(FlashlightListener listener) {
        if (null != listener) {
            synchronized (mListenerLock) {
                listener.onFlashlightStatusChange(isOn());
                flashlightListeners.add(listener);
            }
        }
    }

    private void statusChange() {
        synchronized (mListenerLock) {
            if (!flashlightListeners.isEmpty()) {
                for (FlashlightListener listener : flashlightListeners) {
                    listener.onFlashlightStatusChange(isOn());
                }
            }
        }
    }

    private void initCamera() {
        mCamera = Camera.open();
        if (null != mCamera) {
            Log.d(TAG, "Camera is opened");
            Camera.Parameters parameters = mCamera.getParameters();
            mIsSupportFlashMode = null != parameters.getSupportedFlashModes();
        }
    }

    public boolean isOn() {
        return mIsOn;
    }

    public boolean isSupportFlashMode() {
        return mIsSupportFlashMode;
    }

    public void release() {
        if (null != mCamera) {
            mCamera.release();
        }
    }


    private void turnFlashLightOn() {
        if (null != mCamera && mIsSupportFlashMode) {
            Camera.Parameters parameters = mCamera.getParameters();
            String flashMode = parameters.getFlashMode();
            if (!Camera.Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                mCamera.setParameters(parameters);
                setIsOn(true);
                Log.d(TAG, "Flash light is on");
            }
        }
    }

    private void setIsOn(boolean isOn) {
        mIsOn = isOn;
        statusChange();
    }

    private void turnFlashLightOff() {
        if (null != mCamera && mIsSupportFlashMode) {
            Camera.Parameters parameters = mCamera.getParameters();
            String flashMode = parameters.getFlashMode();
            if (!Camera.Parameters.FLASH_MODE_OFF.equals(flashMode)) {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                mCamera.setParameters(parameters);
                setIsOn(false);
                Log.d(TAG, "Flash light is off");
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (isOn()) {
            Log.d(TAG, "Turning camera off");
            turnFlashLightOff();
        } else {
            Log.d(TAG, "Turning camera on");
            turnFlashLightOn();
        }
        flashView.onFlashlightStatusChange(isOn());
    }

    public void setFlashView(AbstractFlashView view) {
        flashView = view;
        flashView.getControlButton().setOnClickListener(this);
    }

    public interface FlashlightListener {
        public void onFlashlightStatusChange(boolean status);
    }
}
